/*
 *@Description: 操作数据库方法
 *@Author:  lupan
 *@Date: 2019-07-14 14:04:20
*/
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '106.13.12.121',
  user:'root', 
  password:'root', 
  port:'3306', 
  database:'happybook'
});

// 连接公用方法
function query (sql, options, callback) {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, conn) => {
      if(err){
        console.log(err)
      }else{
          conn.query(sql,(err, results ,fields)  => {
              //释放连接  
              if (err) {
                resolve({code: false, message: '服务器连接错误'})
              } else {
                resolve({code: true, data: results})
              }
              conn.release();
          });
      }
    });
  }).catch(err => {
    reject(err)
  })
};

module.exports = query